/*
File: MainMenuState.cpp
Author: Zachary Murray
Purpose: State for the main menu
*/

#include "MainMenuState.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "Game.h"
#include "BitmapFont.h"
#include "Menu.h"
#include "GamePlayState.h"
#include "OptionsState.h"
#include "HowToState.h"
#include "CreditsState.h"
#include "LoadState.h"
#include "MenuCursor.h"
#include <vector>
#include <fstream>
#include <sstream>
#include <istream>
#include "../TinyXML/tinyxml.h"
#include "../SGD Wrappers/SGD_Event.h"

MainMenuState* MainMenuState::GetInstance()
{
	static MainMenuState instance;
	return &instance;
}

void MainMenuState::Enter()
{
	TiXmlDocument doc;
	doc.LoadFile("resources/xml/config.xml");
	TiXmlElement*root = doc.RootElement();
	root->Attribute("MusVol", &musV);
	root->Attribute("SFXVol", &sfxV);
	string fs = root->Attribute("FullScreen");
	if (fs == "0" && Game::GetInstance()->GetWindowed())
		Game::GetInstance()->ChangeWindowed();
	
	/*std::fstream in("resources/options.txt");
	std::string volumeData;
	if (in.is_open())
	{
		getline(in, volumeData, '\0');
		in.close();
	}
	std::istringstream volume(volumeData);
	volume >> sfxV;
	volume.ignore(INT_MAX, ',');
	volume >> musV;*/
	SGD::AudioManager::GetInstance()->SetMasterVolume(
		SGD::AudioManager::VolumeType::MASTER_SOUND_EFFECTS, sfxV);
	SGD::AudioManager::GetInstance()->SetMasterVolume(
		SGD::AudioManager::VolumeType::MASTER_MUSIC, musV);

	std::fstream in;
	in.open("resource/ZSM_save.txt");
	std::string saveData;
	if (in.is_open())
	{
		getline(in, saveData, '\0');
		in.close();
	}


	SGD::GraphicsManager::GetInstance()->SetClearColor({ 0,0,0});
	menu = new Menu;
	menu->Initialize("resources/xml/mainmenu.xml");
	std::vector<SGD::Point> p;
	for (unsigned int i = 0; i < menu->menuItems.size(); i++)
	{
		p.push_back(menu->menuItems[i].GetPosition());
	}
	cursor = new MenuCursor;
	cursor->Initialize(p);
	cursor->SetOffset({ -115, 15 });
	cursor->SetSize({ 5, 5 });
	cursor->SetPosition(cursor->GetPositions()[0]);

	logo = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/logo_alpha.png");
	background = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/shadowmancer_menu.png");

}

void MainMenuState::Exit()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(logo);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(background);
	delete menu;
	menu = nullptr;
	delete cursor;
	cursor = nullptr;
}

bool MainMenuState::Input()
{
	SGD::InputManager* input = SGD::InputManager::GetInstance();
	if (input->IsKeyPressed(SGD::Key::Down) == true
		|| input->IsDPadPressed(0, SGD::DPad::Down))
	{
		SGD::Event e = { "MENUMOVE" };
		e.SendEventNow(nullptr);
		cursPos++;
		if ((unsigned int)cursPos > cursor->GetPositions().size() - 1)
			cursPos = 0;
	}
	if (input->IsKeyPressed(SGD::Key::Up) == true
		|| input->IsDPadPressed(0, SGD::DPad::Up))
	{
		SGD::Event e = { "MENUMOVE" };
		e.SendEventNow(nullptr);
		cursPos--;
		if (cursPos < 0)
			cursPos = cursor->GetPositions().size() - 1;
	}
	if (input->IsKeyPressed(SGD::Key::Enter) == true
		|| input->IsKeyPressed(SGD::Key::LButton) == true
		|| input->IsButtonPressed(0, 0))
	{
		SGD::Event e = { "MENUSELECT" };
		e.SendEventNow(nullptr);

		if (cursPos == 0)
		{
			if (!GamePlayState::GetInstance()->GetPlayed())
			{
				GamePlayState::GetInstance()->SetPlayed(true);
				HowToState::GetInstance()->SetToGame(true);
				Game::GetInstance()->ChangeState(HowToState::GetInstance());
			}
			else
				Game::GetInstance()->ChangeState(GamePlayState::GetInstance());
			return true;
		}
		else if (cursPos == 1)
		{
			Game::GetInstance()->ChangeState(LoadState::GetInstance());
			return true;
		}
		else if (cursPos == 2)
		{
			Game::GetInstance()->ChangeState(OptionsState::GetInstance());
			return true;
		}
		else if (cursPos == 3)
		{
			Game::GetInstance()->ChangeState(HowToState::GetInstance());
			return true;
		}
		else if (cursPos == 4)
		{
			Game::GetInstance()->ChangeState(CreditsState::GetInstance());
			return true;
		}
		else if (cursPos == 5)
		{
			Game::GetInstance()->ChangeState(CreditsState::GetInstance());
			return false;
		}
	}

	if (input->IsAnyKeyPressed())
		mouseInput = false;
	else if (input->GetMouseMovement() != SGD::Vector{ 0, 0 })
		mouseInput = true;

	return true;
}

void MainMenuState::Update(float elapsedTime)
{
	if (!mouseInput)
		cursor->SetPosition(cursor->GetPositions()[cursPos]);
	else
	{
		SGD::InputManager* input = SGD::InputManager::GetInstance();
		SGD::Rectangle rect{
			input->GetMousePosition(), SGD::Size{ 1, 1 } };
		for (unsigned int i = 0; i < menu->menuItems.size(); i++)
		{
			if (menu->menuItems[i].buttons[0].GetRect().IsIntersecting(rect))
			{
				if (cursPos != i)
				{
					SGD::Event e = { "MENUMOVE" };
					e.SendEventNow(nullptr);
				}
				cursor->SetPosition(cursor->GetPositions()[i]);
				cursPos = i;
			}
		}
	}
	
	menu->Update(elapsedTime);
}

void MainMenuState::Render()
{
	SGD::GraphicsManager::GetInstance()->DrawTexture(background, { }, {}, {}, {}, {1,1});
	SGD::GraphicsManager::GetInstance()->DrawTexture(logo, { 100, -100 }, {}, {}, {}, { .5f, .5f });
	BitmapFont* font = Game::GetInstance()->GetFont();
	SGD::InputManager* input = SGD::InputManager::GetInstance();
	font->Draw("Main Menu", 5, 5, 1.0f, { 255, 255, 255 });
	menu->Render();
	cursor->Render();
}